import requests
import json, cv2
from base64 import b64decode
import numpy as np
import logging


def result_post():
    url = "http://8.134.53.114:8277/visual/uavAiAlarm/msgUpload"
    json_data = {}
    """南水直播检测报警测试"""
    logging.info("|INFO: post data to {}|".format(url))
    ret = requests.post(url=url, json=json_data)
    status_code = ret.status_code
    if status_code != 200:
        logging.error("|ERR: code {}, result_post fail|".format(status_code))


def post_test():
    json_data = {
        "id": 0,
        "images": [
            "http://36.138.254.186:888/ftp/ftp_test/meter/2/data/20240319092124.jpg",
            "http://36.138.254.186:888/ftp/ftp_test/meter/2/data/20240319092204.jpg",
            # bda7c90d016d3c9ff45abffa4574fb8   85405094bbd0e1f7bf31b87067ad607
            # "http://36.138.254.186:888/ftp/ftp_test/meter/4/data/DJI_20240723145416_0019_V.JPG",
            # "http://36.138.254.186:888/ftp/ftp_test/meter/4/data/DJI_20240723144938_0008_V.JPG",
            # "http://36.138.254.186:888/ftp/ftp_test/meter/4/data/DJI_0403.JPG",
            # "http://36.138.254.186:888/ftp/ftp_test/meter/3/data/微信图片_20240711153639.jpg",
            # "http://36.138.254.186:888/ftp/ftp_test/meter/3/data/微信图片_20240711153145.jpg",
            # /ftp_test/meter/2/data/747b8410b5b09583ef20037967e920b.jpg
            # /ftp_test/meter/2/data/86ff36b740a0293d0d066363e662eee.jpg
        ],
        # "show_score":1,
    }
    url = "http://0.0.0.0:8011/meter_detect"

    request = requests.post(url=url,json = json_data)
    with open("data/output/temp_file.json","w") as f:
        jsondata = json.loads(request.text)
        json.dump(jsondata,f,ensure_ascii=False,indent=4)

    print(request)

"http://8.134.53.114:888/ftp/building_change/train_a.png"


def post_test_building_change():
    json_data = {
        "id": 0,
        "images": [
            [
                "http://8.134.53.114:888/ftp/building_change/train_a.png",
                "http://8.134.53.114:888/ftp/building_change/train_b.png",
            ]
        ],
    }
    url = "http://0.0.0.0:2682/building_change"

    request = requests.post(url=url, json=json_data)
    with open("data/output/temp_file.json", "w") as f:
        jsondata = json.loads(request.text)
        json.dump(jsondata, f)

    print(request)


def rtsp_post():
    json_data = {
        "rtmp_push_path": "rtmp://218.94.155.86:1936/live/livestream_74_3",
        # "rtmp_push_path":"rtmp://218.94.155.86:1936/live/livestream_8_69439858747240f292120e6af0b6a28d",
        "rtsp_origin": "rtmp://218.94.155.86:1936/live/livestream_74_1",
    }
    # json_data = {
    #         "clientip":"192.168.1.22",
    #         "format": "json",
    #         "data":None,
    #         "results": None,
    #         "classify_ms":0,
    #         "wait_in_queue_ms":0
    #     }

    url = "http://0.0.0.0:2682/deepstream_rtsp_detect"

    request = requests.post(url=url, json=json_data)
    print(request)


def stop_rtmp():
    json_data = {"rtmp_push_path": "rtmp://218.94.155.86:1936/live/livestream_74_3"}
    url = "http://0.0.0.0:2682/stop_deepstream_rtsp_detect"

    request = requests.post(url=url, json=json_data)
    print(request)


if __name__ == "__main__":
    post_test()
    # post_test_building_change()
    # result_post()
    # rtsp_post()
    # stop_rtmp()
